﻿package mortal.game.view.copy.ZZXYBattle
{
    import com.greensock.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.mainUI.Navbar.*;

    public class ZZXYBattleRewardCard extends GSprite
    {
        private var _spOpenContainer:GSprite;
        private var _bgOpen:GImageBitmap;
        private var _spCloseContainer:GSprite;
        private var _bgClose:GImageBitmap;
        private var _txtItemName:GTextFiled;
        private var _item:BaseItem;
        private var _isLocked:Boolean = false;
        private var _status:int = -1;
        private var _tweening:Boolean = false;
        private var _tween:TweenMax;
        private var _index:int = 0;
        private var _rewardData:ZZXYBattleCardData;
        private var _openCallback:Function;
        private var _openFinished:Function;
        public var clickOpenRightNow:Boolean = true;
        public var openedFlyToPack:Boolean = true;
        private var _flyId:int = -1;
        private var _times:int = 0;

        public function ZZXYBattleRewardCard()
        {
            return;
        }// end function

        public function get index() : int
        {
            return this._index;
        }// end function

        public function set index(param1:int) : void
        {
            this._index = param1;
            return;
        }// end function

        public function reset() : void
        {
            this.disposeFly();
            if (this._tween != null && this._tween.active)
            {
                this._tween.kill();
            }
            this._tween = null;
            this._tweening = false;
            this._isLocked = false;
            this._status = -1;
            DisplayUtil.removeMe(this._spOpenContainer);
            this._spCloseContainer.transform.matrix3D = null;
            this._spCloseContainer.x = 66;
            this._spOpenContainer.transform.matrix3D = null;
            this._spOpenContainer.x = 66;
            this.addChild(this._spCloseContainer);
            this.filters = [];
            return;
        }// end function

        public function openCard() : void
        {
            this.buttonMode = false;
            this._status = 1;
            DisplayUtil.removeMe(this._spCloseContainer);
            this._spCloseContainer.transform.matrix3D = null;
            this._spCloseContainer.x = 66;
            this.addChild(this._spOpenContainer);
            this._spOpenContainer.transform.matrix3D = null;
            this._spOpenContainer.x = 66;
            if (this._rewardData != null && this._rewardData.belongsToMe)
            {
                this.filters = [FilterConst.guideFilter];
            }
            else
            {
                this.filters = [];
            }
            return;
        }// end function

        public function updateItem(param1:ZZXYBattleCardData) : void
        {
            this._rewardData = param1;
            this._item.itemCode = param1.itemCode;
            this._item.amount = this._rewardData.num;
            this._txtItemName.htmlText = "<font color=\'" + this._item.itemData.itemInfo.colorStr + "\'>" + this._item.itemData.itemInfo.name + "X" + param1.num + "</font>";
            DisplayUtil.removeMe(this._spOpenContainer);
            this.addChild(this._spCloseContainer);
            this._status = -1;
            return;
        }// end function

        public function get isOpened() : Boolean
        {
            return this._status > 0;
        }// end function

        public function set openCallback(param1:Function) : void
        {
            this._openCallback = param1;
            return;
        }// end function

        public function set openFinished(param1:Function) : void
        {
            this._openFinished = param1;
            return;
        }// end function

        public function set locked(param1:Boolean) : void
        {
            this._isLocked = param1;
            return;
        }// end function

        public function set openImg(param1:String) : void
        {
            this._bgOpen.imgUrl = param1;
            return;
        }// end function

        public function set closeImg(param1:String) : void
        {
            this._bgClose.imgUrl = param1;
            return;
        }// end function

        public function get item() : BaseItem
        {
            return this._item;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            this.mouseChildren = true;
            this.mouseEnabled = true;
            this._spOpenContainer = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this._spOpenContainer.x = 66;
            this._spCloseContainer = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this._spCloseContainer.x = 66;
            this.addChild(this._spCloseContainer);
            this._bgOpen = UIFactory.gImageBitmap(ImagesConst.ZZXYBattleCardBg, -66, 0, this._spOpenContainer);
            this._bgClose = UIFactory.gImageBitmap(ImagesConst.ZZXYBattleCardBlack, -66, 0, this._spCloseContainer);
            var _loc_1:* = GlobalStyle.textFormatPutong.center();
            _loc_1.size = 14;
            this._txtItemName = UIFactory.gTextField("", -66, 3, 127, 24, this._spOpenContainer, _loc_1);
            this._item = UIFactory.baseItem(33 - 66, 46, 65, 65, this._spOpenContainer);
            this._item.isShowLock = true;
            this.configEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler);
            this.configEventListener(MouseEvent.MOUSE_OUT, this.mouseOutHandler);
            this.configEventListener(MouseEvent.CLICK, this.clickMeHandler);
            this._status = -1;
            this._tweening = false;
            this.buttonMode = true;
            return;
        }// end function

        public function clickMeHandler(event:MouseEvent) : void
        {
            if (this._isLocked || this.isOpened || this._tweening)
            {
                return;
            }
            this.openCardByClick();
            return;
        }// end function

        private function openCardByClick() : void
        {
            if (this._tweening)
            {
                return;
            }
            if (this.clickOpenRightNow)
            {
                this.startRolling();
            }
            if (this._openCallback != null)
            {
                this._openCallback.apply(null, [this]);
            }
            return;
        }// end function

        public function startRolling() : void
        {
            this._tweening = true;
            this.addChild(this._spCloseContainer);
            DisplayUtil.removeMe(this._spOpenContainer);
            this._spCloseContainer.rotationY = 0;
            this._spCloseContainer.x = 66;
            this._tween = TweenMax.to(this._spCloseContainer, 0.5, {rotationY:90, onComplete:this.onRotate1});
            return;
        }// end function

        private function onRotate1() : void
        {
            DisplayUtil.removeMe(this._spCloseContainer);
            this._spCloseContainer.transform.matrix3D = null;
            this._spCloseContainer.x = 66;
            this.addChild(this._spOpenContainer);
            this._spOpenContainer.rotationY = -90;
            this._tween = TweenMax.to(this._spOpenContainer, 0.5, {rotationY:0, onComplete:this.onRotateEnd});
            if (this._openFinished != null)
            {
                this._openFinished.apply();
            }
            return;
        }// end function

        private function onRotateEnd() : void
        {
            this._spOpenContainer.transform.matrix3D = null;
            this._spOpenContainer.x = 66;
            this._tween = null;
            this._tweening = false;
            this.disposeFly();
            if (this.openedFlyToPack)
            {
                this.flyToBackpack();
            }
            return;
        }// end function

        private function disposeFly() : void
        {
            if (this._flyId > 0)
            {
                clearTimeout(this._flyId);
            }
            this._flyId = -1;
            this._times = 0;
            return;
        }// end function

        private function flyToBackpack() : void
        {
            var _loc_6:String = this;
            var _loc_7:* = this._times + 1;
            _loc_6._times = _loc_7;
            if (this._times > 50)
            {
                this._flyId = -1;
                this._times = 0;
                return;
            }
            if (this._item == null || isDisposed)
            {
                return;
            }
            if (this._item.bitmapdata == null)
            {
                this._flyId = setTimeout(this.flyToBackpack, 200);
            }
            var _loc_1:Array = [];
            var _loc_2:Array = [];
            var _loc_3:* = Math.min(14, this._rewardData.num);
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                if (this._item.bitmapdata == null)
                {
                }
                else
                {
                    _loc_1.push(this._item.bitmapdata.clone());
                    _loc_2.push(this._item.localToGlobal(GameConst.yellowPoint));
                }
                _loc_4++;
            }
            var _loc_5:* = (GameController.navbar.view as NavbarModule).getNavbarIconGlobalPoint(ModuleType.Pack);
            FlyToNavbarTool.flyToAnyPointPatch(_loc_1, _loc_2, _loc_5, 0, 0.4);
            return;
        }// end function

        private function mouseOverHandler(event:MouseEvent) : void
        {
            if (this.isOpened)
            {
                return;
            }
            if (this._isLocked)
            {
                this.filters = [];
            }
            else
            {
                this.filters = [FilterConst.guideFilter];
            }
            return;
        }// end function

        private function mouseOutHandler(event:MouseEvent) : void
        {
            if (this.isOpened)
            {
                return;
            }
            this.filters = [];
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.clickOpenRightNow = true;
            this.openedFlyToPack = true;
            this.disposeFly();
            this._spOpenContainer.dispose(param1);
            this._spOpenContainer.transform.matrix3D = null;
            this._spOpenContainer = null;
            this._spCloseContainer.dispose(param1);
            this._spCloseContainer.transform.matrix3D = null;
            this._spCloseContainer = null;
            this._bgOpen.dispose(param1);
            this._bgOpen = null;
            this._bgClose.dispose(param1);
            this._bgClose = null;
            this._txtItemName.dispose(param1);
            this._txtItemName = null;
            this._item.dispose(param1);
            this._item = null;
            if (this._tween != null && this._tween.active)
            {
                this._tween.kill();
            }
            this._tween = null;
            this._tweening = false;
            this._isLocked = false;
            this._status = -1;
            this.filters = [];
            this.buttonMode = false;
            return;
        }// end function

    }
}
